package net.borlis.errors.providers.implementation.enumbase;

import net.borlis.errors.exceptions.ExceptionKeyMaster;
import net.borlis.errors.exceptions.IExceptionKeyMaster;
import net.borlis.errors.exceptions.ServerException;
import net.borlis.errors.providers.common.IServerExceptionProcessor;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net
 */
@ApplicationScoped @EnumErrors @Deprecated
public class EnumServerExceptionProcessor implements IServerExceptionProcessor {

    @Inject
    private Logger logger;

    @PostConstruct
    public void init(){
        logger.info("init enum based server exceptions");
    }

    @Override
    public ServerException newServerException(String code) {
        return new ServerException(new ExceptionKeyMaster(code, ExceptionCodeKeyDictionary.findKeyByCode(code)));
    }

    @Override
    public ServerException newServerException(String code, Throwable cause) {
        return new ServerException(new ExceptionKeyMaster(code, ExceptionCodeKeyDictionary.findKeyByCode(code)), cause);
    }

    @Override
    public Set<IExceptionKeyMaster> getSupportedCodes() {
        Set<IExceptionKeyMaster> result = new HashSet<>();
        for (ExceptionCodeKeyDictionary code : ExceptionCodeKeyDictionary.values()) {
            result.add(new ExceptionKeyMaster(code.getCode(),code.getKey()));
        }
        return result;
    }

}
